package Zoo.Handler;


/**
 * 各个用户登录界面
 */

import Zoo.Entity.*;
import Zoo.Impl.*;
import Zoo.Service.*;
import Zoo.View.*;
import Zoo.View.LeaderView.LeaderView;
import Zoo.View.LeaderView.LeaderView1;
import Zoo.View.RegisterView.RegisterView;

import Zoo.View.UserView.UserView;
import Zoo.View.ZoomanView.ZoomenView;
import Zoo.View.ZoomanView.ZoomenView1;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.sql.SQLException;

public class LoginHandler extends KeyAdapter implements ActionListener {
    private Loginview loginview;
    private UserServiceImpl userServiceImpl;

    public LoginHandler(Loginview loginview){
        this.loginview=loginview;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text =jButton.getText();
        if("重置".equals(text)){
            loginview.getUsertxt().setText("");
            loginview.getPwdField().setText("");
        }else if("登录".equals(text)){
            try {
                //检查有没有选身份
                check();
                //选了不同身份，则调用不同身份的登录方法
                if(loginview.getFlag()==1){
                    loginUser();
                }else if(loginview.getFlag()==2){
                    loginVisitor();
                }else if(loginview.getFlag()==3){
                    loginLeader();
                }else if(loginview.getFlag()==4){
                    loginZoomen();
                }
//                if(loginview.getFlag()!=0){
//                    Login lo = new Login();
//                    lo.login();
//                }

            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }else if("注册".equals(text)){
            new RegisterView();
            loginview.dispose();
        }
    }
    //
    private void  check(){
        String user = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();
        //校验密码和用户名是否为空，是否选择身份
        if(user==null||"".equals(user.trim())||chars==null||loginview.getFlag()==0){
            JOptionPane.showMessageDialog(loginview,"用户名和密码必填，身份必选");
        }

    }

    private void loginUser() throws SQLException {
        String user = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();
        UserEntity userEntity=new UserEntity();

        //校验密码和用户名是否为空
        if(user==null||"".equals(user.trim())||chars==null||loginview.getFlag()==0){
            JOptionPane.showMessageDialog(loginview,"用户名和密码必填，身份必选");
            return;
        }

        String pwd =new String(chars);
        System.out.println(user+":"+pwd);

        //用户登录
    //    AdminService adminService = new AdminServiceImpl();
        UserService userService=new UserServiceImpl();

        AdminEntity adminEntity=new AdminEntity();
        userEntity.setUsername(user);
        userEntity.setPwd(pwd);
        boolean flag=userService.validateUser(userEntity);
        if(flag){

            //跳转到用户的主界面并销毁登录界面
            //把当前登录用户的用户名传过去主界面
            String userName=loginview.getUsertxt().getText();
            new UserView(userName);


            loginview.dispose();

        }else{
            JOptionPane.showMessageDialog(loginview,"用户名和密码错误");
        }
    }



    private void loginVisitor() throws SQLException {
        String visitorName = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();

        //校验密码和用户名是否为空
        if(visitorName==null||"".equals(visitorName.trim())||chars==null||loginview.getFlag()==0){
            JOptionPane.showMessageDialog(loginview,"用户名和密码必填，身份必选");
            return;
        }

        String idNumber =new String(chars);
        System.out.println(visitorName+":"+idNumber);

        //参观者登录
        VisitorService visitorService=new VisitorServiceImpl();
        VisitorEntity visitorEntity=new VisitorEntity();
        visitorEntity.setVisitorName(visitorName);
        visitorEntity.setIdNumeber(idNumber);
        boolean flag = visitorService.validateVisitor(visitorEntity);


        if(flag){
            //跳转到参观者的主界面并销毁登录界面

            new VisitorView();
            loginview.dispose();

        }else{
            JOptionPane.showMessageDialog(loginview,"用户名和密码错误");
        }
    }

    private void loginZoomen() throws SQLException {
        String zoomenName = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();

        //校验密码和用户名是否为空
        if(zoomenName==null||"".equals(zoomenName.trim())||chars==null||loginview.getFlag()==0){
            JOptionPane.showMessageDialog(loginview,"用户名和密码必填，身份必选");
            return;
        }

        String idNumber =new String(chars);
        System.out.println(zoomenName+":"+idNumber);

        //动物饲养员登录

        ZoomenService zoomenService=new ZoomenServiceImpl();

        //  AdminEntity adminEntity=new AdminEntity();

        ZoomenEntity zoomenEntity=new ZoomenEntity();
        zoomenEntity.setZoomenName(zoomenName);
        zoomenEntity.setIdNumeber(idNumber);
        boolean flag = zoomenService.validateZoomen(zoomenEntity);


        if(flag){
            //跳转到动物饲养员的两个主界面并销毁登录界面
            String zoomenName1=loginview.getUsertxt().getText();

            new ZoomenView(zoomenName1);
            new ZoomenView1(zoomenName1);
            loginview.dispose();

        }else{
            JOptionPane.showMessageDialog(loginview,"用户名和密码错误");
        }
    }
    private void loginLeader() throws SQLException {
        String leaderName = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();

        //校验密码和用户名是否为空
        if(leaderName==null||"".equals(leaderName.trim())||chars==null||loginview.getFlag()==0){
            JOptionPane.showMessageDialog(loginview,"用户名和密码必填，身份必选");
            return;
        }

        String idNumber =new String(chars);
        System.out.println(leaderName+":"+idNumber);

        //园长登录


        LeaderService leaderService=new LeaderServiceImpl();


        LeaderEntity leaderEntity=new LeaderEntity();
        leaderEntity.setLeaderName(leaderName);
        leaderEntity.setIdNumeber(idNumber);
        boolean flag = leaderService.validateLeader(leaderEntity);

        if(flag){
            //跳转到园长主界面并销毁登录界面

                new LeaderView();
                new LeaderView1();
                loginview.dispose();
        }else{
            JOptionPane.showMessageDialog(loginview,"用户名和密码错误");
        }
    }
}
